<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>发送剪贴板内容</title>
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .card {
            border-radius: 1rem;
            margin-top: 2rem;
        }
        .btn {
            width: 100%;
        }
        #progressContainer {
            display: none;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="card shadow-sm">
        <div class="card-body">
            <h4 class="card-title mb-3 text-center">📤 发送剪贴板内容</h4>
            <form id="uploadForm">
                <div class="mb-3">
                    <label for="textContent" class="form-label">文本内容</label>
                    <textarea class="form-control" id="textContent" name="text_content" rows="4" placeholder="输入要发送的文本..." style="resize: none;"></textarea>
                </div>
                <div class="mb-3">
                    <label for="fileInput" class="form-label">文件上传</label>
                    <input type="file" class="form-control" id="fileInput" name="file">
                </div>
                <div class="d-grid gap-2">
                    <button type="submit" id="submitBtn" class="btn btn-primary">发送</button>
                </div>
                <div class="mt-2 text-center small text-muted" id="statusMsg"></div>
                <div id="progressContainer" class="mt-3">
                    <div class="progress">
                        <div id="progressBar" class="progress-bar progress-bar-striped progress-bar-animated" style="width: 0%">0%</div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    const form = document.getElementById('uploadForm');
    const submitBtn = document.getElementById('submitBtn');
    const statusMsg = document.getElementById('statusMsg');
    const progressBar = document.getElementById('progressBar');
    const progressContainer = document.getElementById('progressContainer');
    const textContent = document.getElementById('textContent');
    const fileInput = document.getElementById('fileInput');

    // 二选一逻辑
    textContent.addEventListener('input', () => {
        fileInput.disabled = !!textContent.value.trim();
    });
    fileInput.addEventListener('change', () => {
        textContent.disabled = fileInput.files.length > 0;
    });

    form.addEventListener('submit', function(e) {
        e.preventDefault();

        const formData = new FormData(form);

        if (!formData.get("text_content") && fileInput.files.length === 0) {
            statusMsg.innerText = "❗ 请输入文本或选择一个文件";
            return;
        }

        submitBtn.disabled = true;
        submitBtn.innerText = "发送中...";
        statusMsg.innerText = "上传中...";
        progressBar.style.width = "0%";
        progressBar.innerText = "0%";
        progressContainer.style.display = "block";

        const xhr = new XMLHttpRequest();
        xhr.open('POST', '/clipboard/send', true);

        xhr.upload.addEventListener('progress', function(e) {
            if (e.lengthComputable) {
                const percent = Math.round(e.loaded / e.total * 100);
                progressBar.style.width = percent + '%';
                progressBar.innerText = percent + '%';
            }
        });

        xhr.onload = function() {
            if (xhr.status === 200) {
                statusMsg.innerText = "✅ 上传成功！";
                form.reset();
                textContent.disabled = false;
                fileInput.disabled = false;
            } else {
                statusMsg.innerText = "❌ 上传失败：" + xhr.statusText;
            }
            submitBtn.disabled = false;
            submitBtn.innerText = "发送";
        };

        xhr.onerror = function() {
            statusMsg.innerText = "❌ 网络错误";
            submitBtn.disabled = false;
            submitBtn.innerText = "发送";
        };

        xhr.send(formData);
    });
</script>
</body>
</html>
